package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exception.LyException;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.service.CategoryBrandService;
import com.leyou.item.service.CategoryService;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.awt.geom.RectangularShape;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 虎哥
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {


    @Autowired
    private CategoryBrandService categoryBrandService;
    @Override
    public List<CategoryDTO> queryByBid(long brandId) {
        //1.根据Id查询中间表
        List<CategoryBrand> cbList = categoryBrandService.query().eq("brand_id", brandId).list();
        if(CollectionUtils.isEmpty(cbList)){
            // 资源不存在
            throw new LyException(404, "品牌没有关联的分类！");
        }
        //2.得到一个id的集合
        List<Long> ids = cbList.stream().map(CategoryBrand::getCategoryId).collect(Collectors.toList());
        System.out.println("ids = " + ids);
        //3.根据ids批量查询category表
        List<Category> categoryList = listByIds(ids);
        //4.转换DTO到PO
        System.out.println();
        return CategoryDTO.convertEntityList(categoryList);
    }
}